<!DOCTYPE html>
<html>

<head>
    <title>Online Store Admin Page</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
    <header>
        <h2>Online Store Admin page</h2>
    </header>
    <main>
        <div class="content">
            <h3>
                <span>You are currently login as admin</span>
                <button type="button" class="styledBtn" onclick="window.location.href='LoginPage.html'">Logout</button>
            </h3>
        </div>
        <div class="content">
            <h2 id="top">Products management</h2>
            <hr>
            <div>
                <form action="adminSearch.php" method="POST" class="searchForm">
                    <label>Search for product:</label>
                    <input type="text" name="search" placeholder="Enter product id" required>
                    <button type="submit">Search</button>
                </form>
                <a href="#add" style="float:right;">jump to bottom</a>
                <br><br>
            </div>
            <!--table to display items-->
            <?php
            include "connectStoreDB.php";

            $sql = "SELECT * FROM products;";

            $result = mysqli_query($conn, $sql);

            //check if result is empty
            if (mysqli_num_rows($result) > 0) {

                //print the heading of table
                echo "<table border='1'>";
                echo "<tr>";

                //print each column name
                while ($property = mysqli_fetch_field($result)) {
                    echo "<th>" . $property->name . "</th>";
                }
                echo "<th colspan='2'>Actions</th></tr>";

                //get number of columns
                $fields = mysqli_num_fields($result);

                //Get each row data
                while ($row = mysqli_fetch_array($result)) {
                    echo "<tr>";
                    for ($f = 0; $f < $fields; $f++) {
                        echo "<td>" . $row[$f] . "</td>";
                    }
                    echo "<td><button type=\"button\" onclick=\"deleteItem('" . $row[0] . "')\">Delete this product</button></td>";
                    echo "<td><button type=\"button\" onclick=\"editPrice('" . $row[0] . "')\">Edit product price</button></td></tr>";
                }

                echo "</table>";
                echo mysqli_num_rows($result) . " results";
            } else {
                echo "0 results";
            }

            //close connection
            mysqli_close($conn);
            ?>
            <br>
            <a href="#top">back to top</a>
            <hr>
            <h2 id="add">Add products</h2>
            <!--form to add items-->
            <form action="addProd.php" method="POST" class="logNregForm">
                <input type="text" name="pri" placeholder="Input product price" required><br><br>
                <input type="text" name="cat" placeholder="Input product category"><br><br>
                <button type="submit">Add</button>
            </form>
        </div>
    </main>

    <script>
        function deleteItem(prod) {
            if (confirm("Are you sure to delete " + prod + "?")) {
                location.href = "delProd.php?prod='" + prod + "'";
            }
        }
        function editPrice(prod){
            let newprice=prompt("Enter a new price");
            location.href="editPrice.php?prod="+prod+"&price="+newprice;
        }
    </script>
</body>

</html>